from selenium import webdriver
from selenium.webdriver.common.by import By
from time import sleep
from pprint import pprint

driver = webdriver.Chrome()
driver.get('https://mail.qq.com/')
driver.maximize_window()
"""
userinfo = {'account': '2868266666@qq.com', 'password': 'xiaotaiyang,,'}
# 定位QQ登录方式的位置,并点击
driver.find_element(By.ID, 'qqLoginTab').click()
# 切换登录框架
driver.switch_to.frame('login_frame')
# 定位邮箱账号输入框,并清空上一次登录的账号
driver.find_element(By.ID, 'u').clear()
# 输入邮箱账号
driver.find_element(By.ID, 'u').send_keys(userinfo['account'])
# 定位密码输入框,输入密码
driver.find_element(By.ID, 'p').send_keys(userinfo['password'])
# 定位授权登录按钮,并点击
driver.find_element(By.ID, 'login_button').click()
# 等待10秒,手动验证图形验证码登录
sleep(10)
# 获取登录后的cookies
cookies = driver.get_cookies()
# 获取成功登录后的cookies,用于后面使用
pprint(cookies)
"""

# 通过上面方法得到登录成功后的cookies1
# 请求QQ邮箱网址后,清除原有cookies,导入cookies1,刷新
driver.delete_all_cookies()  # 先清空cookies

# 准备好通过上面方法得到登录成功后的cookies1
cookies1 = [{'domain': '.mail.qq.com',
             'httpOnly': False,
             'name': 'ssl_edition',
             'path': '/',
             'secure': False,
             'value': 'sail.qq.com'},
            {'domain': '.mail.qq.com',
             'httpOnly': False,
             'name': 'new_mail_num',
             'path': '/',
             'secure': False,
             'value': '-1426700630&68'},
            {'domain': '.mail.qq.com',
             'expiry': 1655371983,
             'httpOnly': False,
             'name': 'edition',
             'path': '/',
             'secure': False,
             'value': 'mail.qq.com'},
            {'domain': '.mail.qq.com',
             'expiry': 1655371983,
             'httpOnly': False,
             'name': 'qm_logintype',
             'path': '/',
             'secure': False,
             'value': 'qq'},
            {'domain': '.mail.qq.com',
             'httpOnly': False,
             'name': 'sid',
             'path': '/',
             'secure': False,
             'value': '-1426700630&4a7933e615bd5905ca61b7d6ef8911b0,qcjBITkVnYXVGRUxXRW9TYWtqNC1Fb05tRlNZZzgySDlpRDFaeVh1cE1NUV8.'},
            {'domain': '.mail.qq.com',
             'httpOnly': False,
             'name': 'pt4_token',
             'path': '/',
             'secure': False,
             'value': 'N5gnye5eVG6GlcJCA94p7svMjjEZEqHkNNG7bRQWouc_'},
            {'domain': '.mail.qq.com',
             'httpOnly': False,
             'name': 'xm_skey',
             'path': '/',
             'secure': False,
             'value': '13102663893141162&9b3e74ab602394e807c8373f53bbcf65'},
            {'domain': '.mail.qq.com',
             'expiry': 1684315988,
             'httpOnly': False,
             'name': 'webp',
             'path': '/',
             'secure': False,
             'value': '1'},
            {'domain': '.mail.qq.com',
             'httpOnly': False,
             'name': 'xm_data_ticket',
             'path': '/',
             'secure': False,
             'value': '13102663893141162&CAESIB2MbVAbFBPXgGoDnja_K9Jou6HWLVKsGfQPXCCLHWLG'},
            {'domain': '.mail.qq.com',
             'httpOnly': False,
             'name': 'p_skey',
             'path': '/',
             'secure': True,
             'value': 'r0HNEgauFELWEoSakj4-EoNmFSYg82H9iD1ZyXupMMQ_'},
            {'domain': '.mail.qq.com',
             'httpOnly': False,
             'name': 'qm_domain',
             'path': '/',
             'secure': False,
             'value': 'https://mail.qq.com'},
            {'domain': '.mail.qq.com',
             'httpOnly': False,
             'name': 'xm_sid',
             'path': '/',
             'secure': False,
             'value': 'zapHMoxGUU4u9ng5AKpScgAA'},
            {'domain': '.mail.qq.com',
             'expiry': 1655371983,
             'httpOnly': False,
             'name': 'qm_device_id',
             'path': '/',
             'secure': False,
             'value': '+fnT94sWYe+SOVXMeUvXG8F1wBCVZC94wvodHINzuzgfIKSUUJ+/+BMt4iPM0M/T'},
            {'domain': '.mail.qq.com',
             'httpOnly': False,
             'name': 'username',
             'path': '/',
             'secure': False,
             'value': '-1426700630&2868266666'},
            {'domain': '.mail.qq.com',
             'httpOnly': False,
             'name': 'qm_username',
             'path': '/',
             'secure': False,
             'value': '2868266666'},
            {'domain': '.mail.qq.com',
             'expiry': 1655371987,
             'httpOnly': False,
             'name': 'CCSHOW',
             'path': '/',
             'secure': False,
             'value': '000001'},
            {'domain': '.qq.com',
             'httpOnly': False,
             'name': 'uin',
             'path': '/',
             'secure': False,
             'value': 'o2868266666'},
            {'domain': '.qq.com',
             'httpOnly': False,
             'name': 'skey',
             'path': '/',
             'secure': False,
             'value': '@eZvpbTTzs'},
            {'domain': '.mail.qq.com',
             'httpOnly': False,
             'name': 'xm_uin',
             'path': '/',
             'secure': False,
             'value': '13102663893141162'},
            {'domain': '.qq.com',
             'expiry': 2147483651,
             'httpOnly': False,
             'name': 'ptcz',
             'path': '/',
             'secure': False,
             'value': '37c301ac6571037f522b53282f1703c78b7a6bac69b0d79ffb3811759f5ceed5'},
            {'domain': '.mail.qq.com',
             'httpOnly': False,
             'name': 'p_uin',
             'path': '/',
             'secure': False,
             'value': 'o2868266666'},
            {'domain': '.mail.qq.com',
             'httpOnly': False,
             'name': 'xm_muti_sid',
             'path': '/',
             'secure': False,
             'value': '13102663893141162&zapHMoxGUU4u9ng5AKpScgAA'},
            {'domain': '.qq.com',
             'expiry': 2147483651,
             'httpOnly': False,
             'name': 'RK',
             'path': '/',
             'secure': False,
             'value': '6P+QfBkV6I'},
            {'domain': '.qq.com',
             'expiry': 1655371985,
             'httpOnly': False,
             'name': 'ptui_loginuin',
             'path': '/',
             'secure': False,
             'value': '2868266666@qq.com'}]

# 遍历添加cookies1的cookie
for cookie in cookies1:
    driver.add_cookie(cookie)

# 刷新页面成功进入登录后的页面
driver.refresh()
# 加入隐式等待
driver.implicitly_wait(5)

# 定位写信按钮,点击
driver.find_element(By.LINK_TEXT, '写信').click()
# 切换frame
driver.switch_to.frame('mainFrame')
# 定位收件人,输入
driver.find_element(By.XPATH, '//div[@id="toAreaCtrl"]/div[2]/input').send_keys('my.sadness@163.com')
# 定位主题,输入
driver.find_element(By.XPATH, '//input[@id="subject"]').send_keys('Test')
# 切换frame
text_frame = driver.find_element(By.XPATH, '//iframe[@class="qmEditorIfrmEditArea"]')
driver.switch_to.frame(text_frame)
# 定位内容框,输入
driver.find_element(By.XPATH, '/html/body/div').send_keys('day day up!')
# 切换到默认frame
driver.switch_to.default_content()
# 切换frame
driver.switch_to.frame('mainFrame')
# 定位发送按钮,点击
driver.find_element(By.LINK_TEXT, '发送').click()

# 退出浏览器
sleep(3)
driver.quit()
